package edu.its.dreamteam.web.pages.admin;

import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.ioc.annotations.Inject;

import edu.its.dreamteam.daf.beans.Formation;
import edu.its.dreamteam.daf.dao.IFormationDao;
import edu.its.dreamteam.web.base.SecureBasePage;

public class FormationForm extends SecureBasePage {
	@Inject
	private IFormationDao formationDao;
	@Persist
	private Formation formation;		
	@Component
	private Form formationForm;
	
	private String idFormation;
	
	public IFormationDao getFormationDao() {
		return formationDao;
	}
	
	
	public Formation getFormation() {
		return formation;
	}


	public void setFormation(Formation formation) {
		this.formation = formation;
	}


	void onPrepare(){
        if (formation == null){
        	formation = new Formation();
        	
        }
    }
	
	void cleanupRender() {
		setFormation(null);
	}
	
	void onActionFromClear(){
		setFormation(null);
		formationForm.clearErrors();
    }
		
	Object onSuccessFromFormationForm(){
		if(getFormation().getIdFormation()==null){
			getFormationDao().insert(getFormation());
		}else{
			getFormationDao().update(getFormation());
		}
		setFormation(null);
		return FormationGrid.class;
	}
	
	void onActivate(String idFormation){
		this.idFormation = idFormation;	
		setFormation((Formation) getFormationDao().findByPrimaryKey(idFormation));			
	}
	
	String onPassivate(){ 		
		return idFormation;
	}	
	
	
}
